﻿using AngleSharp.Io;
using Furion;
using Furion.FriendlyException;
using Furion.Templates;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.IdentityModel.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection.PortableExecutable;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace GWB.Core.Common
{
    public static class HttpHelper
    {

     
        /// <summary>
        /// Get 请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postDataStr"></param>
        /// <param name="headers"></param>
        /// <param name="platform"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public static string HttpGet(string url, string postDataStr, Dictionary<string, string> headers,string platform, string contentType = "")
        {
         
            var result = string.Empty;
            try
            {





                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + postDataStr);

                if (Convert.ToBoolean(App.Configuration["HttpProxy:IsEnableProxy"]))
                {
                    WebProxy myProxy = new WebProxy();
                    string proxyAddress = App.Configuration["HttpProxy:ProxyAddress"];
                    string username = App.Configuration["HttpProxy:UserName"];
                    string password = App.Configuration["HttpProxy:PassWord"];

                  
                    WebProxy proxy = new WebProxy(proxyAddress, Convert.ToInt32(App.Configuration["HttpProxy:Port"]));
                    proxy.Credentials = new NetworkCredential(username, password);
                    request.Proxy = proxy;
                }
                
                request.Method = "GET";
                request.ContentType = contentType;
                foreach (KeyValuePair<string, string> kv in headers)
                {
                    request.Headers.Add(kv.Key, kv.Value);
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                result = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

            }
            catch (Exception ex)
            {
                throw Oops.Oh($"{platform}"+ ex.Message);
            }
            return result;
        }

        public static string PostData(string url, string postData, Encoding encoding, string ContentType, Dictionary<string, string> headers)
        {

            try
            {
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
                if (Convert.ToBoolean(App.Configuration["HttpProxy:IsEnableProxy"]))
                {
                    WebProxy myProxy = new WebProxy();
                    string proxyAddress = App.Configuration["HttpProxy:ProxyAddress"];
                    string username = App.Configuration["HttpProxy:UserName"];
                    string password = App.Configuration["HttpProxy:PassWord"];

                    WebProxy proxy = new WebProxy(proxyAddress, Convert.ToInt32(App.Configuration["HttpProxy:Port"]));
                    proxy.Credentials = new NetworkCredential(username, password);
                    myReq.Proxy = proxy;
                }


                myReq.Method = "POST";
                myReq.Timeout = 600 * 60;
                byte[] byteArray = encoding.GetBytes(postData);
                myReq.ContentType = ContentType;
                //对键值对进行遍历
                foreach (KeyValuePair<string, string> kv in headers)
                {
                    myReq.Headers.Add(kv.Key, kv.Value);
                }
                Stream dataStream = myReq.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                dataStream = HttpWResp.GetResponseStream();
                string result = "";
                using (StreamReader myStreamReader = new StreamReader(dataStream, encoding))
                {
                    result = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                }
                dataStream.Close();
                HttpWResp.Close();
                return result;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
    }
}
